package com.lc.zpyh.view;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.Html;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.lc.zpyh.R;
import com.lc.zpyh.http.response.SelectUserCounpsUsableBean;
import com.lc.zpyh.util.BigDecimalUtils;

import org.jetbrains.annotations.NotNull;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.ButterKnife;

public class CouponDialog  extends Dialog implements View.OnClickListener {

    private Context context;
    private ImageView img_close;

    private RecyclerView rv_coupon;
    List<SelectUserCounpsUsableBean.ListBean> dataarrtBeanList ;
    private CouponDialog.OnGoodsOrderListener onGoodsOrderListener;
    private BaseQuickAdapter<SelectUserCounpsUsableBean.ListBean, BaseViewHolder> couponAdapter;
    private String price;
    private String couponId;

    public CouponDialog(Context context, List datalist) {
        super(context, R.style.dialog);
        this.context = context;
        this.dataarrtBeanList=datalist;


    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);

        Window window = getWindow();
        window.setGravity(Gravity.BOTTOM); // 此处可以设置dialog显示的位置为居中
//        window.setWindowAnimations(R.style.bottom_menu_animation); // 添加动画效果
        setContentView(R.layout.dialog_order_coupon);
        WindowManager windowManager = ((Activity) context).getWindowManager();
//        Display display = windowManager.getDefaultDisplay();
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        DisplayMetrics d = context.getResources().getDisplayMetrics(); // 获取屏幕宽、高
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        getWindow().setAttributes(lp);
        setCanceledOnTouchOutside(false);// 点击Dialog外部消失



        initView();


    }
    private void initView() {
     
        rv_coupon = findViewById(R.id.rv_coupon);
        img_close = findViewById(R.id.img_close);

        img_close.setOnClickListener(this);



        couponAdapter = new BaseQuickAdapter<SelectUserCounpsUsableBean.ListBean,
                BaseViewHolder>(R.layout.item_coupon, dataarrtBeanList) {

            @Override
            protected void convert(BaseViewHolder holder, SelectUserCounpsUsableBean.ListBean bean) {
                holder.setVisible(R.id.ll_to_make,true);
                holder.setText(R.id.tv_address, bean.getSenderName());

                holder.setText(R.id.tv_money, BigDecimalUtils.div(bean.getCreditFor()+"","100",2)+"");
                holder.setText(R.id.tv_date, Html.fromHtml("满"+BigDecimalUtils.div(bean.getMeetTheConditions()+"","100",0)+"可用<br>有效期至："+bean.getEndtime()+"<br/>"));
            }


        };
        couponAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull @NotNull BaseQuickAdapter<?, ?> adapter, @NonNull @NotNull View view, int position) {

              price= BigDecimalUtils.div(dataarrtBeanList.get(position).getCreditFor()+"","100",2);
                couponId = dataarrtBeanList.get(position).getUserPreferentialId()+"";
                if (onGoodsOrderListener != null) {
                    onGoodsOrderListener.onBuyNow(price,couponId);
                    dismiss();
                }

            }
        });
        rv_coupon.setAdapter(couponAdapter);

    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {

            case R.id.img_close:


                if (onGoodsOrderListener != null) {
                    onGoodsOrderListener.onDismiss("不使用优惠券");
                    dismiss();
                }
                break;



            default:
                break;
        }
    }
    public interface OnGoodsOrderListener {
        void onDismiss( String time);


        void onBuyNow( String time,String couponId);


    }

    public void setOnGoodsOrderListener(CouponDialog.OnGoodsOrderListener onGoodsOrderListener) {
        this.onGoodsOrderListener = onGoodsOrderListener;
    }
}
